﻿<%@ WebHandler Language="C#" Class="addSlider" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间

public class addSlider : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.Write(formDataUpload(context));
    }

    private static string formDataUpload(HttpContext context)
    {

        HttpFileCollection postedFile = context.Request.Files;
        bool fileUploaded = false;
        string name = context.Request.Form["name"];
        string mid = context.Request.Form["mid"];
        string intro = context.Request.Form["intro"];
        string imgPath = "";

        string msg = string.Empty;
        string error = string.Empty;

        // "++"

        if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(intro))
        {
            return "{\"error\":\"2\", \"msg\":\"表单项不能为空，请重新填写！\"}";
        }

        if (postedFile.Count > 0)
        {
            string savepath = "";
            string tempPath = "";
            tempPath = System.Web.HttpContext.Current.Server.MapPath("..\\upload\\");//System.Configuration.ConfigurationManager.AppSettings["FolderPath"];  
            savepath = tempPath; //context.Server.MapPath(tempPath);  

            string filename = postedFile[0].FileName;
            if (!String.IsNullOrEmpty(filename))
            {
                string sExtension = filename.Substring(filename.LastIndexOf('.'));

                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);

                // string sNewFileName = filename.Substring(0, filename.IndexOf("."));


                string sNewFileName = Guid.NewGuid().ToString("N");
                imgPath = @"upload/" + sNewFileName + sExtension;
                postedFile[0].SaveAs(savepath + @"\" + sNewFileName + sExtension);
                msg = " 成功! ";
                error = "0";
                fileUploaded = true;
            }
            else
            {
                msg = " 文件未选择! ";
                error = "1"; 
            }

        }
        else
        {
            msg = "没有选择文件";
            error = "1";
            return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
        }

        if (fileUploaded)
        {
            DBHelper.sqlMethodNoDataset("insert into sliders(s_name, s_image, s_intro) values('" + name + "','" + imgPath + "','" + intro + "');");
        }
        else
        {
            msg = " 上传失败! ";
            error = "1";
        }


        return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";

    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}